package com.monitor.methods;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.EthernetPacket;
import jpcap.packet.ICMPPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.TCPPacket;

public class ICMPMethod implements Method {

	private int srcPort = 135;
	private int start;
	private int stop;
	private InetAddress address;

	public ICMPMethod(String address, int start, int stop)
			throws UnknownHostException {
		super();
		this.address = InetAddress.getByName(address);
		this.start = start;
		this.stop = stop;
	}
	
	@Override
	public void startMonitor() throws IOException {
		// TODO Auto-generated method stub
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		int index = 0;
		System.out.println("Wybierz interfejs: \n");
		for (int i = 0; i < devices.length; i++) {
			// print out its name and description
			System.out.println(i + ": " + devices[i].name + "("
					+ devices[i].description + ")");

			// print out its datalink name and description
			System.out.println(" datalink: " + devices[i].datalink_name + "("
					+ devices[i].datalink_description + ")");

			// print out its MAC address
			System.out.print(" MAC address:");
			for (byte b : devices[i].mac_address)
				System.out.print(Integer.toHexString(b & 0xff) + ":");
			System.out.println();

			// print out its IP address, subnet mask and broadcast address
			for (NetworkInterfaceAddress a : devices[i].addresses) {
				System.out.println(" address:" + a.address + " " + a.subnet
						+ " " + a.broadcast);
			}
			System.out.println();
		}
		Scanner sc = new Scanner(System.in);
		Integer interf = sc.nextInt();
		
		JpcapSender sender = JpcapSender.openDevice(devices[interf]);
		
		JpcapCaptor captor = JpcapCaptor.openDevice(devices[interf], 100000, true, 10);
		 captor.setFilter ( "icmp", true); 
		
		for (int i = start; i <= stop; i++) {
		
			ICMPPacket p=new ICMPPacket();
			p.type=ICMPPacket.ICMP_ECHO;
			p.seq=1000;
			p.id=999;
			p.orig_timestamp=123;
			p.trans_timestamp=456;
			p.recv_timestamp=789;
			p.setIPv4Parameter(0,false,false,false,0,false,false,false,0,1010101,100,IPPacket.IPPROTO_ICMP,
				InetAddress.getByName("192.168.0.27"), address);
			p.data="data".getBytes();
	
			EthernetPacket ether=new EthernetPacket();
			ether.frametype=EthernetPacket.ETHERTYPE_IP;
			ether.src_mac=new byte[]{(byte)0,(byte)1,(byte)2,(byte)3,(byte)4,(byte)5};
			ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9,(byte)10};
			p.datalink=ether;
	
			//for(int i=0;i<10;i++)
			sender.sendPacket(p);
			int k=0;
			while (true) {
				ICMPPacket returnPacket = (ICMPPacket) captor.getPacket();

				System.out.println(returnPacket);
				/* If the packet captor times out set the return value to false */
				if (returnPacket == null) {
					if (++k >= 5) {
						System.out.println(k+" Zamkniety");
						break;
					}
					continue;
				} else {
	
					/*
					 * If we received a return packet from the remote machine
					 * set the return value to true
					 */
				
					System.out.println(returnPacket);
				}
			}
		}
	}
	
}
